package com.example.calendarapp;

import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import android.widget.ExpandableListView;
import android.widget.Toast;


/**
 * This class consists of several methods that allows 
 * the calendar's weekly view to be created. It populates the 
 * days of the week and adds the times of each day in weekly view. 
 *
 * @author Emilio Celis-Aguirre
 * @author Jeremy Henderson
 * @author Felipe Luna
 * @author Nathan Whitaker
 * @author Xiaoxu Zhang
 */

public class WeekFragmentAdapter extends BaseExpandableListAdapter {

	private Context context;
	private EventsDataSource data;
	private Calendar c;
	private String[] hours = {"12AM", "1AM", "2AM", "3AM", "4AM", "5AM", "6AM", "7AM", "8AM",
				"9AM", "10AM", "11AM", "12PM", "1PM", "2PM", "3PM", "4PM", "5PM",
				 "6PM", "7PM", "8PM", "9PM", "10PM", "11PM"};
	private String[] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
	private SimpleDateFormat df;
	private List<String> dates;
	private List<ArrayList<Event>> events;

	/**
	 * This method formats and adds data
	 * for the calendar and events.
	 * @param ctx	Context ctx
	 */
	
	public WeekFragmentAdapter(Context ctx)
	{
		c = Calendar.getInstance();
		df = new SimpleDateFormat("yyyy-MM-dd");
		this.context = ctx;
		data = EventsDataSource.getInstance(ctx);
		data.createDatabase();
		data.open();
		dates = new ArrayList<String>();
		events = new ArrayList<ArrayList<Event>>();
		int delta = -c.get(GregorianCalendar.DAY_OF_WEEK) + 1;
		c.add(Calendar.DAY_OF_MONTH, delta);
		buildDates();
	}
	
	/**
	 * The method returns needed information about the event.
	 * @param arg0 
	 * @param arg1
	 * @return events.get(arg0).get(arg1);
	 */
	
	@Override
	public Event getChild(int arg0, int arg1) {
		return events.get(arg0).get(arg1);
	}
	
	/**
	 * The method gets the id of the child, it is not used.
	 * @param arg0 
	 * @param arg1
	 * @return 0
	 */

	@Override
	public long getChildId(int arg0, int arg1) {
		return 0;
	}

	
	/**
	 * This method inflates the child view, and returns it.
	 * @param parentPpsition
	 * @param childPosition
	 * @param boolean
	 * @param childView
	 * @param ParentView
	 * @return convertView
	 */
	
	@Override
	public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
			ViewGroup arg4)
	{
		LayoutInflater inflater = LayoutInflater.from(context);
		View convertView = inflater.inflate(R.layout.week_item, null);
		TextView dayItemName = (TextView) convertView.findViewById(R.id.dayItemName);
		TextView dayItemHour = (TextView) convertView.findViewById(R.id.dayItemHour);
		dayItemName.setText(events.get(arg0).get(arg1).getName());
		dayItemName.setTextColor(Resources.getSystem().getColor(events.get(arg0).get(arg1).getColor()));
		dayItemHour.setText(hours[events.get(arg0).get(arg1).getHourS()]);
		ExpandableListView e = (ExpandableListView) arg4;
		e.expandGroup(arg0);
		return convertView;
	}

	/**
	 * This method gets and returns the count for a parent view.
	 * @param parent
	 * @return events.get(arg0).size()
	 */
	
	@Override
	public int getChildrenCount(int arg0) {
		return events.get(arg0).size();
	}

	@Override
	public Object getGroup(int arg0) {
		return null;
	}
	
	/**
	 * This method gets and returns the number of groups, (always 7 days in week).
	 * @return 7  
	 */

	@Override
	public int getGroupCount() {
		return 7;
	}

	@Override
	public long getGroupId(int arg0) {
		return 0;
	}
	
	/**
	 * This method inflates each group view, sets each group as always expanded.
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return convertView
	 */

	@Override
	public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) {
		LayoutInflater inflater = LayoutInflater.from(context);
		View convertView = inflater.inflate(R.layout.week_item_group, null);
		TextView weekGroupDay = (TextView) convertView.findViewById(R.id.weekGroupDay);
		TextView weekGroupDate = (TextView) convertView.findViewById(R.id.weekGroupDate);
		
		weekGroupDay.setText(days[arg0]);
		weekGroupDate.setText(dates.get(arg0));
		ExpandableListView e = (ExpandableListView) arg3;
		e.expandGroup(arg0);
		return convertView;
	}
	
	/**
	 * This method increments the week, and gets new dates and events
	 */	
	public void incrementWeek()
	{
		c.add(Calendar.DATE, 7);
		buildDates();
	}
	
	/**
	 * This method decrements the week and gets new dates and events
	 */
	
	public void decrementWeek()
	{
		c.add(Calendar.DATE, -7);
		buildDates();
	}
	
	/**
	 * This method gets events from the database and adds them to the list, while also building date objects
	 */
	
	public void buildDates()
	{
		events.clear();
		dates.clear();
		for (int i = 0; i < 7; i++) {
			dates.add(df.format(c.getTime()));
			events.add(data.getDayEvents(df.format(c.getTime()), c.get(Calendar.DAY_OF_WEEK)-1));
			c.add(Calendar.DAY_OF_MONTH, 1);
		}
		c.add(Calendar.DAY_OF_MONTH, -7);
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		return true;
	}
	
	/**
	 * This method gets the datestring based on the input of a row
	 * @param row
	 * @return dateString
	 */

	public String getDateString(int row)
	{
		c.add(Calendar.DATE, row);
		String datestring = df.format(c.getTime());
		c.add(Calendar.DATE, row*-1);
		return datestring;
	}
	
}
